package com.example.sachon;

import java.io.InputStream;
import java.net.URL;
import java.util.List;



import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

public class DetailContentListAdapter extends BaseAdapter {

	private static final String LOGTAG = "DetailConList";
	private LayoutInflater inflater;
	private List<DetailContentItem> list;

	public DetailContentListAdapter(Context context, List<DetailContentItem> list) {
		inflater = LayoutInflater.from(context);
		this.list = list;
	}

	public int getCount() {
		return list.size();
	}

	public Object getItem(int arg0) {
		return list.get(arg0);
	}

	public long getItemId(int position) {

		return position;
	}

	public View getView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		
		Log.d(LOGTAG, " position : " + position);

		if (list.get(position).getMode() == 0) {//내용
			if(convertView == null){
				viewHolder = new ViewHolder();
				convertView = inflater.inflate(R.layout.list_row, null);
				
				viewHolder.no = (TextView) convertView.findViewById(R.id.no);
				viewHolder.subject = (TextView) convertView.findViewById(R.id.subject);
				viewHolder.no.setVisibility(viewHolder.no.VISIBLE);
				viewHolder.subject.setVisibility(viewHolder.subject.VISIBLE);				
				viewHolder.image = (ImageView) convertView.findViewById(R.id.image);
				viewHolder.image.setVisibility(viewHolder.image.GONE);
								
				convertView.setTag(viewHolder);
				
				
			}else{
				viewHolder = (ViewHolder)convertView.getTag();
				
				viewHolder.no.setVisibility(viewHolder.no.VISIBLE);
				viewHolder.subject.setVisibility(viewHolder.subject.VISIBLE);
				viewHolder.image.setVisibility(viewHolder.image.GONE);
				
			}
			
			viewHolder.no.setText(list.get(position).getUser_name() + "\n" + list.get(position).getWr_dt());
			viewHolder.subject.setText(list.get(position).getContent());
			
			Log.d(LOGTAG, " 내용 댓글 mode  : "+ list.get(position).getMode());
			
			
			
			
			return convertView;
		} else if (list.get(position).getMode() == 1) {//이미지
			if(convertView == null){
				viewHolder = new ViewHolder();
				convertView = inflater.inflate(R.layout.list_row, null);
				
				viewHolder.no = (TextView) convertView.findViewById(R.id.no);
				viewHolder.subject = (TextView) convertView
						.findViewById(R.id.subject);
				viewHolder.no.setVisibility(viewHolder.no.GONE);
				viewHolder.subject.setVisibility(viewHolder.subject.GONE);
								
				viewHolder.image = (ImageView) convertView
						.findViewById(R.id.image);
				viewHolder.image.setVisibility(viewHolder.image.VISIBLE);
				
				
				Log.d(LOGTAG, " Set ImageView.ScaleType.FIT_START");
				viewHolder.image.setScaleType(ImageView.ScaleType.FIT_XY);
								
				convertView.setTag(viewHolder);
	
				Log.d(LOGTAG, " Image convertView is not NULL..");
			}else{
				viewHolder = (ViewHolder)convertView.getTag();
				
				viewHolder.no.setVisibility(viewHolder.no.GONE);
				viewHolder.subject.setVisibility(viewHolder.subject.GONE);
				viewHolder.image.setVisibility(viewHolder.image.VISIBLE);
				viewHolder.image.setScaleType(ImageView.ScaleType.CENTER_CROP);				
				
			}
			
			try {
				String urlString = "http://auralizer.cafe24.com/upload/" + list.get(position).getPic_names();
				System.out.println(urlString);
				System.out.println(position + ">>>>>" + list.get(position).getPic_names());
				
				ImageDownloader.download(urlString, viewHolder.image);
				
			} catch (Exception e) {
				e.printStackTrace();
			}
			
			return convertView;
		} else if (list.get(position).getMode() == 2) {//댓글
			if(convertView == null){
				viewHolder = new ViewHolder();
				convertView = inflater.inflate(R.layout.list_row, null);
				
				viewHolder.no = (TextView) convertView.findViewById(R.id.no);
				viewHolder.subject = (TextView) convertView
						.findViewById(R.id.subject);
				viewHolder.no.setVisibility(viewHolder.no.VISIBLE);
				viewHolder.subject.setVisibility(viewHolder.subject.VISIBLE);
				
				viewHolder.image = (ImageView) convertView
						.findViewById(R.id.image);
				viewHolder.image.setVisibility(viewHolder.image.GONE);
				
								
				convertView.setTag(viewHolder);
	
				Log.d(LOGTAG, " 댓글일 경우 "+ list.get(position).getMode());
			}else{
				viewHolder = (ViewHolder)convertView.getTag();
				
				viewHolder.no.setVisibility(viewHolder.no.VISIBLE);
				viewHolder.subject.setVisibility(viewHolder.subject.VISIBLE);
				viewHolder.image.setVisibility(viewHolder.image.GONE);
				
				
			}
			
			viewHolder.no.setText(list.get(position).getUser_name());
			viewHolder.subject.setText(list.get(position).getSubcontent());
							
			return convertView;
		}else{
			return convertView;
		}
	}

	public final class ViewHolder {
		private TextView no;
		private TextView subject;
		private ImageView listReplyImg;
		private TextView listReplyNo;
		private TextView writer;
		private TextView wdate;
		private TextView hit;
		private ImageView image;
	}

}